package cn.edu.tju.apps.utils;

import java.io.File;
import java.util.List;
import java.util.Map;

import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableWorkbook;

public class DailyData {
	public Map<String, Double> dataAu9995 = null;
	public Map<String, Double> dataAu9999 = null;
	public Map<String, Double> dataAu100g = null;
	public Map<String, Double> dataAuTD = null;
	public Map<String, Double> dataPt9995 = null;
	public Map<String, Double> dataAgTD = null;
	public Map<String, Double> dataAg9999 = null;
	public String dateStr = "";
	public static final String outFilePath = "/Users/liudajian/Documents/workspace/GoldSpider/TDDATA.xls";
	public static String [] strSet = {"开盘价", "最高价", "最低价", "收盘价", "涨跌（元）", "涨跌幅", "加权平均价", "成交量", "成交金额"};
	public static boolean isWriting = false;
	
	public boolean setData(String dataType, Map<String, Double> dataMap){
		if("Au9995".equals(dataType) || "Au99.95".equals(dataType)){
			this.dataAu9995 = dataMap;
//			writeFile("Au9995", dataMap, this.dateStr);
		}else if("Au9999".equals(dataType) || "Au99.99".equals(dataType)){
			this.dataAu9999 = dataMap;
//			writeFile("Au9999", dataMap, this.dateStr);
		}else if("Au100g".equals(dataType)){
			this.dataAu100g = dataMap;
//			writeFile("Au100g", dataMap, this.dateStr);
		}else if("Au(T+D)".equals(dataType) || "AuT+D".equals(dataType)){
			this.dataAuTD = dataMap;
//			writeFile("AuTD", dataMap, this.dateStr);
		}else if("Pt9995".equals(dataType)){
			this.dataPt9995 = dataMap;
//			writeFile("Pt9995", dataMap, this.dateStr);
		}else if("Ag(T+D)".equals(dataType) || "AgT+D".equals(dataType)){
			this.dataAgTD = dataMap;
//			writeFile("AgTD", dataMap, this.dateStr);
		}else if("Ag99.99".equals(dataType)){
			this.dataAg9999 = dataMap;
//			writeFile("Ag9999", dataMap, this.dateStr);
		}
		return false;
	}
	
	public static boolean outputList(List<DailyData> dataList){
		if(isWriting){
			return false;
		}
		isWriting = true;
		DailyData dateItem;
		for (int i = 0; i < dataList.size(); i++) {
			System.out.println("writting" + i + " of " + dataList.size());
			dateItem = dataList.get(i);
			if(dateItem.dataAu9995 != null){
				writeFile("Au9995", dateItem.dataAu9995, dateItem.dateStr);
			}
			if(dateItem.dataAu9999 != null){
				writeFile("Au9999", dateItem.dataAu9999, dateItem.dateStr);
			}
			if(dateItem.dataAu100g != null){
				writeFile("Au100g", dateItem.dataAu100g, dateItem.dateStr);
			}
			if(dateItem.dataAuTD != null){
				writeFile("AuTD", dateItem.dataAuTD, dateItem.dateStr);
			}
			if(dateItem.dataPt9995 != null){
				writeFile("Pt9995", dateItem.dataPt9995, dateItem.dateStr);
			}
			if(dateItem.dataAgTD != null){
				writeFile("AgTD", dateItem.dataAgTD, dateItem.dateStr);
			}
			if(dateItem.dataAg9999 != null){
				writeFile("Ag9999", dateItem.dataAg9999, dateItem.dateStr);
			}
		}
		return false;
	}
	
	public static boolean writeFile(String sheetName, Map<String, Double> dataMap, String date){
		try {
			// Excel获得文件
        	Workbook wb = Workbook.getWorkbook(new File(outFilePath));
            
            // 打开一个文件的副本，并且指定数据写回到原文件
            WritableWorkbook book = Workbook.createWorkbook(new File(outFilePath),
                    wb);
            // 添加一个工作表
            int rowNum = book.getSheet(sheetName).getRows()-2;
            System.out.println(rowNum);
            book.getSheet(sheetName).insertRow(rowNum);
            Label dateLabel = new Label(0, rowNum, date);
            dateLabel.setCellFormat(book.getSheet(sheetName).getCell(0, rowNum-1).getCellFormat());
        	book.getSheet(sheetName).addCell(dateLabel);
			
            for (int i = 0; i < strSet.length; i++) {
            	if(dataMap.get(strSet[i]) == null){
            	continue;	
            	}
				jxl.write.Number number = new jxl.write.Number(i+1, rowNum, (Double)dataMap.get(strSet[i]));
	            book.getSheet(sheetName).addCell(number);
			}
            book.write();
            book.close();
        } catch (Exception e) {
            System.out.println(e);
        }
		return false;
	}
}
